<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pengajuan extends CI_Controller {

	function __construct()
    {
        parent::__construct();
		$this->load->model('frontmodel');
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->helper(array('form', 'url'));
		
		if( !$this->session->userdata('isLoggedIn') ) redirect ('front/signin');
		
    }

	public function index()
	{
		$username	= $this->session->userdata('user_name');
		$data['show']	= $this->frontmodel->get_pemohon($username);
		foreach($data["show"] as $row){
			$id	= $row->ID_PEMOHON;
		}
		$this->load->model('m_pengajuan');
		$data['title']		= '| Daftar Pengajuan Legalisir';
		$data['pengajuan']	= $this->m_pengajuan->get_by(array("ID_PEMOHON"=>$id,"STATUS <"=>"4"));
		$data['main']= 'pengajuan/v_pengajuan';
		$this->load->view('v_template',$data);
	}
	public function json_pengajuan($id = FALSE){
		$this->load->model("m_pengajuan");
		$this->load->model("m_user");
		echo '{ "geonames": [';
		if($id == FALSE){
			$join	= array(
				array("table"=>"pengajuan","join_key"=>"ID_PEMOHON","join_table"=>"pemohon")
			);
			$data	= $this->m_pengajuan->get_join($join);			
		} else {
			$user = $this->m_user->get_by(array("USERNAME"=>$this->session->userdata('user_name')),FALSE,FALSE,TRUE);
			$join	= array(
				array("table"=>"pengajuan","join_key"=>"ID_PEMOHON","join_table"=>"pemohon")
			);
			$data	= $this->m_pengajuan->get_join($join,array("pemohon.ID_USER"=>$user["ID_USER"]));
		}
		$i		= 0;
		foreach($data as $row)
		{
			
			$i++;
			if($id == FALSE){
				$button	= '"button": "<a href=\'' . site_url("admin/adlegalisir/lihatPermohonan/$row[ID_PENGAJUAN]") . '\'><button class=\'btn btn-info btn-xs\'><span class=\'fa fa-eye\'></span></button></a>",';
			} else {
				$button	= '"button": "<a href=\'' . site_url("pengajuan/lihatPengajuan/$row[ID_PENGAJUAN]") . '\'><button class=\'btn btn-info btn-xs\'><span class=\'fa fa-eye\'></span></button></a>",';
			}
			
			
			if($i >= count($data))
			{
				if($row['STATUS_TK1'] != 'NF' || $row['STATUS_TK2'] != 'NF' || $row['STATUS_TK3'] != 'NF')
				{
					echo '{
						"id": "' . $i . '",' .
						$button .
						'"ID_PENGAJUAN": "' . $row["ID_PENGAJUAN"] .'",
						"NAMA_LENGKAP": "' . $row["NAMA_LENGKAP"] .'",
						"ID_PEMOHON": "' . $row["ID_PEMOHON"] .'",
						"TGL_PENGAJUAN": "' . date("d-m-Y",strtotime($row["TGL_PENGAJUAN"])) .'",
						"ALASAN": "' . $row["ALASAN"] .'",
						"TGL_VERIFIKASI": "' . $row["TGL_VERIFIKASI"] .'",
						"TGL_VERIFIKASI_KADIN": "' . $row["TGL_VERIFIKASI_KADIN"] .'",
						"TGL_CETAK": "' . $row["TGL_CETAK"] .'",
						"TGL_PENGAMBILAN": "' . (($row["TGL_PENGAMBILAN"] == null || $row["TGL_PENGAMBILAN"] == "" || $row["TGL_PENGAMBILAN"] == "0000-00-00") ? ("-") : (date("d-m-Y",strtotime($row["TGL_PENGAMBILAN"])))) .'",
						"LEMBAR_TK1": "' . $row["LEMBAR_TK1"] .'",
						"LEMBAR_TK2": "' . $row["LEMBAR_TK2"] .'",
						"LEMBAR_TK3": "' . $row["LEMBAR_TK3"] .'",
						"STATUS": "' . (($row["STATUS"] == 1) ? ("Belum diverifikasi") : (($row["STATUS"] == 2) ? ("Sudah diverifikasi, belum siap ambil.") : (($row["STATUS"] == 3) ? ("Sudah diverifikasi, sudah siap ambil.") : ("Sudah diambil.")))) .'",
						"VERIFIKATOR": "' . $row["VERIFIKATOR"] .'",
						"SEKOLAH": "' . (($row["LEMBAR_TK1"] > 0) ? (($row["LEMBAR_TK2"] > 0 || $row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK1"].'</span>' . " ") : ('<span class=\'label label-xs bg-info \'>'.$row["TK1"].'</span>')) : ("")) . (($row["LEMBAR_TK2"] > 0) ? (($row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK2"].'</span>' . " ") : ('<span class=\'label label-xs bg-info \'>'.$row["TK2"].'</span>')) : ("")) . (($row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK3"].'</span>') : ("")) . '"
					}';
				}
			} 
			else 
			{
				if($row['STATUS_TK1'] != 'NF' || $row['STATUS_TK2'] != 'NF' || $row['STATUS_TK3'] != 'NF')
				{
					echo '{
						"id": "' . $i . '",' . 
						$button . 
						'"ID_PENGAJUAN": "' . $row["ID_PENGAJUAN"] .'",
						"NAMA_LENGKAP": "' . $row["NAMA_LENGKAP"] .'",
						"ID_PEMOHON": "' . $row["ID_PEMOHON"] .'",
						"TGL_PENGAJUAN": "' . date("d-m-Y",strtotime($row["TGL_PENGAJUAN"])) .'",
						"ALASAN": "' . $row["ALASAN"] .'",
						"TGL_VERIFIKASI": "' . $row["TGL_VERIFIKASI"] .'",
						"TGL_VERIFIKASI_KADIN": "' . $row["TGL_VERIFIKASI_KADIN"] .'",
						"TGL_CETAK": "' . $row["TGL_CETAK"] .'",
						"TGL_PENGAMBILAN": "' . (($row["TGL_PENGAMBILAN"] == null || $row["TGL_PENGAMBILAN"] == "" || $row["TGL_PENGAMBILAN"] == "0000-00-00") ? ("-") : ($row["TGL_PENGAMBILAN"])) .'",
						"LEMBAR_TK1": "' . $row["LEMBAR_TK1"] .'",
						"LEMBAR_TK2": "' . $row["LEMBAR_TK2"] .'",
						"LEMBAR_TK3": "' . $row["LEMBAR_TK3"] .'",
						"STATUS": "' . (($row["STATUS"] == 1) ? ("Belum diverifikasi") : (($row["STATUS"] == 2) ? ("Sudah diverifikasi, belum siap ambil.") : (($row["STATUS"] == 3) ? ("Sudah diverifikasi, sudah siap ambil.") : ("Sudah diambil.")))) .'",
						"VERIFIKATOR": "' . $row["VERIFIKATOR"] .'",
						"SEKOLAH": "' . (($row["LEMBAR_TK1"] > 0) ? (($row["LEMBAR_TK2"] > 0 || $row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK1"].'</span>' . " ") : ('<span class=\'label label-xs bg-info \'>'.$row["TK1"].'</span>')) : ("")) . (($row["LEMBAR_TK2"] > 0) ? (($row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK2"].'</span>' . " ") : ('<span class=\'label label-xs bg-info \'>'.$row["TK2"].'</span>')) : ("")) . (($row["LEMBAR_TK3"] > 0) ? ('<span class=\'label label-xs bg-info \'>'.$row["TK3"].'</span>') : ("")) . '"
					},';
				}
			}
		}
		echo "] }";
	}
	public function addpermohonan(){
		$this->updateBiodata();
		if(!$this->input->post("paket1") && !$this->input->post("paket2") && !$this->input->post("paket3")){
			?>
			<script language="javascript">
				alert("Pengajuan gagal, silakan coba lagi atau hubungi administrator.");
			</script>
			<?php
		} else {
			$this->tambahPermohonan();
			$this->upload();
		}
		redirect('pengajuan');
	}

	public function editpermohonan()
	{
		$this->updateBiodata();
		$this->upload();
		redirect('pengajuan');
	}
	
	public function upload()
	{
		$count		= 0;
		$enc_dir 	= "1".md5(md5("uploads").":".md5("uploads"));;
		$this->load->model(array("m_pemohon","m_pengajuan"));
		$folder = $this->session->userdata('id_user');
		if (!is_dir($enc_dir.'/' . $folder))
		{
			mkdir('./'.$enc_dir.'/' . $folder, 0777, true);
			$dir_exist = false; // dir not exist
		}
		$id	= $this->session->userdata("id_user");
		$pemohon	= $this->session->userdata("pemohon");
		$id_pengajuan = $this->m_pengajuan->get_by(array("ID_PEMOHON"=>$pemohon),FALSE,FALSE,TRUE,FALSE,"ID_PENGAJUAN DESC");
		for($i=0;$i<8;$i++){ 
			if(isset($_FILES['file_'.$i]['name']))
			{
				if($i == 1){
					$config['file_name'] = "sd_" . $id;
				} else if($i == 2) {
					$config['file_name'] = "smp_" . $id;
				} else if($i == 0) {
					$config['file_name'] = "ktp_" . $id;
				} else if ($i == 3){
					$config['file_name'] = "sma_" . $id;
				} else if ($i == 4){
					$config['file_name'] = "pendukung_" . $id;
				} else if ($i == 5) {
					$config['file_name'] = "sd_back_" . $id;
				} else if ($i == 6) {
					$config['file_name'] = "smp_back_" . $id;
				} else {
					$config['file_name'] = "sma_back_" . $id;
				}
				$config['upload_path'] = "./$enc_dir/$folder/";
				$config['allowed_types'] = 'jpg|png';
				$config['max_size']	= '100000';
				$config['max_width']  = '1024000';
				$config['max_height']  = '768000';    
				$this->load->library('upload');
				$this->upload->initialize($config);
				$sd	= $this->input->post("sd");
				$smp= $this->input->post("smp");
				$sma= $this->input->post("sma");
				if ( !$this->upload->do_upload('file_'.$i)){
					$path[$i] = "";
				} else{
					$file = $this->upload->data();
					$path[$i]=$enc_dir."/" . $folder . "/" . $file['file_name'];
					if($i == 1)
						$this->m_pemohon->save(array("ALAMAT_TK1"=>$path[$i],"TK1"=>$sd),array("ID_USER"=>$id));
					else if($i == 2)
						$this->m_pemohon->save(array("ALAMAT_TK2"=>$path[$i],"TK2"=>$smp),array("ID_USER"=>$id));
					else if($i == 0)
						$this->m_pemohon->save(array("ALAMAT_KTP"=>$path[$i],"TK3"=>$sma),array("ID_USER"=>$id));
					else if($i == 3)
						$this->m_pemohon->save(array("ALAMAT_TK3"=>$path[$i]),array("ID_USER"=>$id));
					else if($i == 4)
						$this->m_pengajuan->save(array("BERKAS_PENDUKUNG"=>$path[$i]),array("ID_PENGAJUAN"=>$id_pengajuan["ID_PENGAJUAN"]));
					else if($i == 5)
						$this->m_pemohon->save(array("ALAMAT_BACK_TK1"=>$path[$i]),array("ID_USER"=>$id));
					else if($i == 6)
						$this->m_pemohon->save(array("ALAMAT_BACK_TK2"=>$path[$i]),array("ID_USER"=>$id));
					else
						$this->m_pemohon->save(array("ALAMAT_BACK_TK3"=>$path[$i]),array("ID_USER"=>$id));
					$count++;
				}
				if($count > 0){
					
				}
				echo $file['file_name'];
			} else {
				
			}
		}
			
		$this->load->model('addpengajuan');
		$this->addpengajuan->alamat($path,$id);
		
		$this->load->helper('url');		
	}

	public function lihatPengajuan($id_pengajuan = null)
	{
		$this->load->model('addpengajuan');
		
		//kalau id_pengajuan kosong, maka data diset NULL
		$username	= $this->session->userdata('user_name');
		$data['show']	= $this->frontmodel->get_pemohon($username);
		if(!isset($id_pengajuan)){
			$data['users'] = null;
		}else{
			if(is_numeric($id_pengajuan))
			{
				$data['users'] = $this->addpengajuan->selectPengajuanByID($id_pengajuan);
			}
			else
			{
				$data['users'] = null;
			}
		}
		$data['title']	= '| Lihat Biodata';
		$data['main']	= 'pengajuan/v_lihatPengajuan';
		$this->load->view('v_template',$data);
	}	
	
	public function tambahPermohonan()
	{
		$id_user = $this->session->userdata('id_user');
		$this->frontmodel->add_pengajuan($id_user);
	}

	public function tambahPermohonan2()
	{
		$this->form_validation->set_rules("alasan","Alasan Pengajuan","required");
		if($this->input->post("account1") <= 0 && $this->input->post("account2") <= 0 && $this->input->post("account3") <= 0){
			$this->form_validation->set_rules("valid","Jumlah legalisir yang diajukan","required");
		}
		$this->form_validation->set_message("required","%s harus diisi.");
		$this->form_validation->set_error_delimiters('<div class="col-sm-3">&nbsp;</div><div class="col-sm-8"><div class="alert alert-danger" style="margin-top:10px;">', '</div></div>');

		if($this->form_validation->run() == TRUE){
			$this->load->model("m_pemohon");
			$this->load->model("m_pengajuan");
			$pemohon	= $this->m_pemohon->get_by(array("ID_USER"=>$this->session->userdata("id_user")),FALSE,FALSE,TRUE);
			$alasan		= $this->input->post("alasan");
			$lembar1	= $this->input->post("account1");
			$lembar2	= $this->input->post("account2");
			$lembar3	= $this->input->post("account3");
			// $verifikator= $this->input->post("verifikator");
			$this->m_pengajuan->save(array("ID_PEMOHON"=>$pemohon["ID_PEMOHON"],"ALASAN"=>$alasan,"TGL_PENGAJUAN"=>date("Y-m-d"),"LEMBAR_TK1"=>$lembar1,"LEMBAR_TK2"=>$lembar2,"LEMBAR_TK3"=>$lembar3,"STATUS"=>1,"VERIFIKATOR"=>(($alasan == "CPNS Pusat") ? (1) : (2))));
			$this->upload();
			redirect('pengajuan'); 	
		} else {
			$id_user = $this->session->userdata('id_user');
			$this->load->model('m_pemohon');
			$this->load->model("m_data_sekolah");
			$this->load->model("m_pengajuan");
			$data["sd"]	= $this->m_data_sekolah->get_by(array("JENJANG"=>"MI","JENJANG"=>"SD"),FALSE,TRUE);
			$data["smp"]	= $this->m_data_sekolah->get_by(array("JENJANG"=>"MTS","JENJANG"=>"SMP"),FALSE,TRUE);
			$data["sma"]	= $this->m_data_sekolah->get_by(array("JENJANG"=>"MA","JENJANG"=>"SMA","JENJANG"=>"SMK"),FALSE,TRUE);
			$username	= $this->session->userdata('user_name');
			$data['show']	= $this->frontmodel->get_pemohon($username);
			$data['data'] = $this->m_pemohon->get_by(array("ID_USER"=>$id_user),FALSE,FALSE,TRUE);
			$data["pengajuan_sd"]	= $this->m_pengajuan->get_by(array("ID_PEMOHON"=>$data["data"]["ID_PEMOHON"],"MONTH(TGL_PENGAJUAN)"=>date("m")),FALSE,FALSE,TRUE,array("SUM(LEMBAR_TK1) AS JUMLAH"));
			$data["pengajuan_smp"]	= $this->m_pengajuan->get_by(array("ID_PEMOHON"=>$data["data"]["ID_PEMOHON"],"MONTH(TGL_PENGAJUAN)"=>date("m")),FALSE,FALSE,TRUE,array("SUM(LEMBAR_TK2) AS JUMLAH"));
			$data["pengajuan_sma"]	= $this->m_pengajuan->get_by(array("ID_PEMOHON"=>$data["data"]["ID_PEMOHON"],"MONTH(TGL_PENGAJUAN)"=>date("m")),FALSE,FALSE,TRUE,array("SUM(LEMBAR_TK3) AS JUMLAH"));
			
			$data['main']= 'pengajuan/v_tambahPermohonan2';
			$this->load->view('v_template',$data);
		}
	}

	public function updateBio()
	{
		$username = $this->session->userdata('user_name');
		
		$data['show'] 	= $this->frontmodel->get_pemohon($username);
		$data['title']	= '| Update Biodata';
		$data['main']	= 'pengajuan/v_updateBiodata';
		$this->load->view('v_template',$data);
	}

	public function updateBiodata()
	{
		$username = $this->session->userdata('user_name');
		$this->frontmodel->updateBio($username);	
	}
	
	public function tandaTerima()
	{
		$this->load->view('pengajuan/v_tandaTerima');
	}

	public function logout()
	{
		$this->session->unset_userdata('logged_in');
		$this->session->sess_destroy();
		$this->load->view('front/v_signin');
	}
}

/* End of file pengajuan.php */
/* Location: ./application/controllers/pengajuan.php */